package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.UUIDUtils;
import cn.stylefeng.guns.sys.modular.system.entity.BtyPaymentOrder;
import cn.stylefeng.guns.sys.modular.system.entity.Enum.PayStateEnum;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyPaymentOrderMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyPaymentOrderParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyPaymentOrderAndYybbResult;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyPaymentOrderResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyPaymentOrderDetailsService;
import  cn.stylefeng.guns.sys.modular.system.service.BtyPaymentOrderService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 店面日营业现金支付详情表 服务实现类
 * </p>
 *
 * @author wjk
 * @since 2021-12-07
 */
@Service
public class BtyPaymentOrderServiceImpl extends ServiceImpl<BtyPaymentOrderMapper, BtyPaymentOrder> implements BtyPaymentOrderService {

    @Autowired
    private BtyPaymentOrderDetailsService btyPaymentOrderDetailsService;

    @Override
    public void add(BtyPaymentOrderParam param){
        BtyPaymentOrder entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BtyPaymentOrderParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BtyPaymentOrderParam param){
        BtyPaymentOrder oldEntity = getOldEntity(param);
        BtyPaymentOrder newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BtyPaymentOrderResult findBySpec(BtyPaymentOrderParam param){
        return null;
    }

    @Override
    public List<BtyPaymentOrderResult> findListBySpec(BtyPaymentOrderParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BtyPaymentOrderParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }


    /**
     * 新建订单，将手动支付的订单存入，并且直接判定支付成功，支付人记录当前登录用户，支付方式为手动添加，增加手动支付人员信息和支付时间
     *
     * @author wjk
     * @Date 2021-12-07
     * @param btyPaymentOrderAndYybbResult
     *
     *
     * @updatDate 2022-01-07
     * @updat  新增店内充值金额，计算汇总
     */
    @Override
    public BtyPaymentOrder addBtyPaymentOrderForPaidCash(BtyPaymentOrderAndYybbResult btyPaymentOrderAndYybbResult) {
        //生成订单号，生成订单
        BtyPaymentOrder btyPaymentOrder = new BtyPaymentOrder();

        LoginUser sysLoginUser = LoginContextHolder.getContext().getUser();

        Date date = new Date();
        //将创建者信息存入
        btyPaymentOrder.setCreateUser(sysLoginUser.getId());
        btyPaymentOrder.setCreateTime(date);
        btyPaymentOrder.setUpdateUser(sysLoginUser.getId());
        btyPaymentOrder.setUpdateTime(date);

        btyPaymentOrder.setYaoma(btyPaymentOrderAndYybbResult.getYaoma());


        btyPaymentOrder.setPaidCashAll(btyPaymentOrderAndYybbResult.getSumTodayCash());

        //支付状态，支付成功
        btyPaymentOrder.setPayState(PayStateEnum.PAY_SUCCESS.getCode());

//
        //支付方式
        btyPaymentOrder.setPayMethod("SDZF");

        //手动支付id
        Long id = UUIDUtils.getId();
        btyPaymentOrder.setId(id);
        btyPaymentOrder.setPayCode(id);

        //增加手动支付信息
        btyPaymentOrder.setManuallyAddUserId(String.valueOf(sysLoginUser.getId()));
        btyPaymentOrder.setManuallyAddUser(sysLoginUser.getName());
        btyPaymentOrder.setManuallyAddDatetime(date);

        Boolean flag = this.save(btyPaymentOrder);
        if(flag){
            return btyPaymentOrder;
        }else{
            return new BtyPaymentOrder();
        }
    }

    @Override
    public List<BtyPaymentOrderAndYybbResult> cashAndPay(BtyPaymentOrderParam param) {
        // todo 店内现金交账开始日期
        String paidCashStartDate = this.btyPaymentOrderDetailsService.generalGetSysDictPaidCashStartDateDay();
        // todo 店内现金交账充值现金开始日期
        String xjbzStartDate = this.btyPaymentOrderDetailsService.generalGetSysDictXjbzStartDateDay();

        List<BtyPaymentOrderAndYybbResult> list = this.baseMapper.calculateCashAndPay(param,paidCashStartDate,xjbzStartDate);

        return list;
    }

    @Override
    public List<BtyPaymentOrderAndYybbResult> getYingYe(BtyPaymentOrderParam param) {


        // todo 店内现金交账开始日期
        String paidCashStartDate = this.btyPaymentOrderDetailsService.generalGetSysDictPaidCashStartDateDay();
        // todo 店内现金交账充值现金开始日期
        String xjbzStartDate = this.btyPaymentOrderDetailsService.generalGetSysDictXjbzStartDateDay();

        List<BtyPaymentOrderAndYybbResult> list = this.baseMapper.Yingye(param,paidCashStartDate,xjbzStartDate);

        return list;
    }

    @Override
    public List<BtyPaymentOrderAndYybbResult> getPayAll(BtyPaymentOrderParam param) {



        List<BtyPaymentOrderAndYybbResult> list = this.baseMapper.payAll(param);
//        List<String> list1=new ArrayList();
//        List<String> list2=new ArrayList();
//        List<String> list3=new ArrayList();
//        List<String> list4=new ArrayList();
//
//        if(list.size()>0){
//            for (BtyPaymentOrderAndYybbResult item:list){
//                list1.add(String.valueOf(item.getPayDay()));
//                list2.add(String.valueOf(item.getXjAll()));
//                list3.add(String.valueOf(item.getXjbzAll()));
//                list4.add(String.valueOf(item.getPayAll()));
//            }
//        }
//
//        map.put("payDay",list1);
//        map.put("xjAll",list2);
//        map.put("xjbzAll",list3);
//        map.put("payAll",list4);

        return list;
    }

    @Override
    public List<BtyPaymentOrderAndYybbResult> getCash(BtyPaymentOrderParam param) {
        // todo 店内现金交账开始日期
        String paidCashStartDate = this.btyPaymentOrderDetailsService.generalGetSysDictPaidCashStartDateDay();
        // todo 店内现金交账充值现金开始日期
        String xjbzStartDate = this.btyPaymentOrderDetailsService.generalGetSysDictXjbzStartDateDay();

        List<BtyPaymentOrderAndYybbResult> list = this.baseMapper.cash(param,paidCashStartDate,xjbzStartDate);

        return list;
    }

    private Serializable getKey(BtyPaymentOrderParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BtyPaymentOrder getOldEntity(BtyPaymentOrderParam param) {
        return this.getById(getKey(param));
    }

    private BtyPaymentOrder getEntity(BtyPaymentOrderParam param) {
        BtyPaymentOrder entity = new BtyPaymentOrder();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
